package com.new_wear.project.system.repair.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.project.system.repair.domain.Repair;
import com.new_wear.project.system.repair.service.IRepairService;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.common.utils.poi.ExcelUtil;
import com.new_wear.framework.web.page.TableDataInfo;

/**
 * 报修信息Controller
 * 
 * @author new_wear
 * @date 2019-12-24
 */
@Controller
@RequestMapping("/system/repair")
public class RepairController extends BaseController
{
    private String prefix = "system/repair";

    @Autowired
    private IRepairService repairService;

    @RequiresPermissions("system:repair:view")
    @GetMapping()
    public String repair()
    {
        return prefix + "/repair";
    }

    /**
     * 查询报修信息列表
     */
    @RequiresPermissions("system:repair:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Repair repair)
    {
        startPage();
        List<Repair> list = repairService.selectRepairList(repair);
        if (list != null) {
            for (Repair repairlsit : list) {
                if (repairlsit.getStatus().equals("0")){
                    repairlsit.setStatus("未处理");
                }else if (repairlsit.getStatus().equals("1")){
                    repairlsit.setStatus("进行中");
                }else if (repairlsit.getStatus().equals("2")){
                    repairlsit.setStatus("已完成");
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出报修信息列表
     */
    @RequiresPermissions("system:repair:export")
    @Log(title = "报修信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Repair repair)
    {
        List<Repair> list = repairService.selectRepairList(repair);
        ExcelUtil<Repair> util = new ExcelUtil<Repair>(Repair.class);
        return util.exportExcel(list, "repair");
    }

    /**
     * 新增报修信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存报修信息
     */
    @RequiresPermissions("system:repair:add")
    @Log(title = "报修信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Repair repair)
    {
        int i = repairService.insertRepair(repair);
        return toAjax(i);
    }

    /**
     * 修改报修信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Repair repair = repairService.selectRepairById(id);
        mmap.put("repair", repair);
        return prefix + "/edit";
    }

    /**
     * 修改保存报修信息
     */
    @RequiresPermissions("system:repair:edit")
    @Log(title = "报修信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Repair repair)
    {
        return toAjax(repairService.updateRepair(repair));
    }

    /**
     * 删除报修信息
     */
    @RequiresPermissions("system:repair:remove")
    @Log(title = "报修信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(repairService.deleteRepairByIds(ids));
    }
}
